package com.foreknow.controller;

import com.foreknow.model.UserEntity;
import com.foreknow.repository.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class MainController {

    @Autowired
    private UserRespository userRespository;

    @RequestMapping(path = "/index",method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/admin/users",method = RequestMethod.GET)
    public  String getUsers(ModelMap modelMap){
        //调用持久层方法查询所有用户的信息
        List<UserEntity> userList = userRespository.findAll();
        //将查询到的UserList保存到modelmap里
        modelMap.addAttribute("userList",userList);
        return "admin/users";
    }

    @RequestMapping(value = "/admin/userdetail",method = RequestMethod.GET)
    public String userDetail(ModelMap modelMap,UserEntity u) {
        modelMap.addAttribute("user",u);
        return "admin/userDetail";
    }


    //跳转到添加用户信息界面
    @RequestMapping(value = "/admin/users/add",method = RequestMethod.GET)
     public String addUser() {
        return "admin/addUser";
    }

    //添加用户信息
    @RequestMapping(value = "/admin/user/addP",method = RequestMethod.POST)
    public String addUserPost(@ModelAttribute("user") UserEntity userEntity){
        //调用持久层方法
        userRespository.saveAndFlush(userEntity);
    return  "redirect:/admin/users";
    }

    //根据id删除用户的信息
    //restful风格：就是一种设计的风格（设计原则或约束条件），而不是标准
    //每一个URI代表一种资源
    //客户端和服务器之间，传递这种资源的某种表现形式
    //客户端通过四个http动作对服务器端的资源进行操作
    // Get(Select)从服务器中取出资源
    //Post(Create)在服务器端创建资源
    //Put(Update)在服务器中更新资源
    //Deletr(delete)从服务器中删除资源
    @RequestMapping(value = "/admin/users/delete/{id}",method = RequestMethod.GET)
    public String deleteUser(@PathVariable("id") Integer userId){
        userRespository.delete(userId);
        userRespository.flush();
        return "redirect:/admin/users";
    }



    //根据id查询用户的信息
    @RequestMapping(value = "admin/users/update/{id}",method = RequestMethod.GET)
    public  String updateUser(@PathVariable("id") Integer id,ModelMap modelMap){
        UserEntity userEntity = userRespository.findOne(id);
        modelMap.addAttribute("user",userEntity);
        return  "admin/updateUser";
    }

    //修改后查询信息
    @RequestMapping(value = "/admin/users/ok",method = RequestMethod.POST)
    public  String getUsersok(@ModelAttribute("name") UserEntity userEntity,ModelMap modelMap){
        //调用持久层保存所有用户的信息
        UserEntity userEntity1 = userRespository.saveAndFlush(userEntity);
        //调用持久层方法查询所有用户的信息
        List<UserEntity> userList = userRespository.findAll();
        //将查询到的UserList保存到modelmap里
        modelMap.addAttribute("userList",userList);
        return "admin/users";
    }

}